//
//  TL_AppraiseDetailHeaderView.m
//  WXReader
//
//  Created by Andrew on 2020/4/13.
//  Copyright © 2020 Andrew. All rights reserved.
//

#import "TL_AppraiseDetailHeaderView.h"

@implementation TL_AppraiseDetailHeaderView
{
    UIImageView *avatarImage;   //头像
    UIImage *avatarHoldImage;
    UILabel *nickNameLabel;     //昵称
    UIImageView *vipImageView;  // VIP标识
    UILabel *timeLabel;         //发布时间
    UILabel *commentLabel;      //评论
    UILabel *commentNumLabel;      //评论
    UIButton *dzBtn;      //评论
    UILabel *replyCommentLabel; // 二级评论
    
    TL_ProductionCoverView *bookImageView;
    
    UIButton *bookBottomView;
    UILabel *bookTitleLabel;
    UILabel *authorLabel;
}

- (void)createSubviews
{
    [super createSubviews];
    
    // 头像
    avatarHoldImage = HoldUserAvatar;
    avatarHoldImage = [avatarHoldImage imageByResizeToSize:CGSizeMake(35.0, 35.0) contentMode:UIViewContentModeScaleAspectFill];
    avatarHoldImage = [avatarHoldImage imageByRoundCornerRadius:35.0 / 2.0];
    avatarImage = [TL_UIFactory imageViewWithBackgroundColor:nil image:avatarHoldImage cornerRadius:0.0];
    [self.contentView addSubview:avatarImage];
    
    [avatarImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).with.offset(kMargin);
        make.top.mas_equalTo(self.contentView.mas_top).with.offset(kMargin);
        make.width.height.mas_equalTo(35.0f);
    }];
    
    // 昵称
    nickNameLabel = [[UILabel alloc] init];
    nickNameLabel.textColor = RGB(77, 77, 75);
    nickNameLabel.font = SYSTEM_FONT(15);
    nickNameLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:nickNameLabel];
    
    [nickNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(avatarImage.mas_right).with.offset(kHalfMargin);
        make.centerY.mas_equalTo(avatarImage.mas_centerY);
        make.right.mas_equalTo(self.contentView.mas_right).with.offset(-kMargin);
        make.height.mas_equalTo(avatarImage.mas_height).multipliedBy(0.6);
    }];
    
//    vipImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"public_vip_normal"]];
//    [self.contentView addSubview:vipImageView];
//    
//    [vipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(timeLabel.mas_right);
//        make.centerY.mas_equalTo(timeLabel.mas_centerY);
//        make.height.mas_equalTo(10);
//        make.width.mas_equalTo(kGeometricWidth(10, 138, 48));
//    }];
    
    // 二级评论
    NSLog(@"二级评论");
    replyCommentLabel = [[UILabel alloc] init];
    replyCommentLabel.textAlignment = NSTextAlignmentLeft;
    replyCommentLabel.textColor = RGB(77, 77, 75);
    replyCommentLabel.font = SYSTEM_FONT(13);
    replyCommentLabel.backgroundColor = kGrayViewColor;
    replyCommentLabel.numberOfLines = 0;
//    replyCommentLabel.textContainerInset = UIEdgeInsetsMake(6, 5, 4, 5);
    [self.contentView addSubview:replyCommentLabel];
    
    [replyCommentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(avatarImage.mas_left);
        make.top.mas_equalTo(avatarImage.mas_bottom);
        make.right.mas_equalTo(self.contentView.mas_right).with.offset(- kMargin);
        make.height.mas_equalTo(CGFLOAT_MIN);
    }];
    
    // 评论
    commentLabel = [[UILabel alloc] init];
    commentLabel.textAlignment = NSTextAlignmentLeft;
    commentLabel.font = SYSTEM_FONT(12);
    commentLabel.numberOfLines = 0;
    commentLabel.textColor = RGB(93, 90, 90);
    [self.contentView addSubview:commentLabel];
    
    [commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(avatarImage.mas_right).with.offset(kHalfMargin);
        make.right.mas_equalTo(self.contentView.mas_right).with.offset(- kMargin);
        make.top.mas_equalTo(replyCommentLabel.mas_bottom).with.offset(kHalfMargin-5);
        make.height.mas_equalTo(200);
    }];
    
    timeLabel = [[UILabel alloc] init];
    timeLabel.textAlignment = NSTextAlignmentLeft;
    timeLabel.font = SYSTEM_FONT(10);
    timeLabel.textColor = kGrayTextColor;
    [self.contentView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(avatarImage.mas_right).with.offset(kHalfMargin);
        make.top.mas_equalTo(commentLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(avatarImage.mas_height).multipliedBy(0.4);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).with.offset(- kHalfMargin).priorityLow();
    }];
    
    commentNumLabel = [[UILabel alloc] init];
    commentNumLabel.textColor = [UIColor colorWithHexString:@"#FF9966"];
    commentNumLabel.font = MK_FONT_PFSC_Medium(14);
    [self.contentView addSubview:commentNumLabel];
    [commentNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView.mas_right).offset(-15);
        make.centerY.mas_equalTo(timeLabel.mas_centerY);
    }];
    
    dzBtn = [TL_UIFactory buttonWithTarget:self action:@selector(dzAction)];
    [dzBtn setImage:[[UIImage imageNamed:@"like-1-unSelected"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    dzBtn.contentEdgeInsets = UIEdgeInsetsMake(7.5, 7.5, 7.5, 7.5);
    dzBtn.tintColor = RGB(153, 153, 153);
    [self.contentView addSubview:dzBtn];
    [dzBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(timeLabel.mas_centerY);
        make.right.equalTo(commentNumLabel.mas_left);
        make.width.height.mas_equalTo(30.0);
    }];
    
//    bookBottomView = [UIButton buttonWithType:UIButtonTypeCustom];
//    bookBottomView.backgroundColor = kGrayViewColor;
//    [bookBottomView addTarget:self action:@selector(commentProductionClick) forControlEvents:UIControlEventTouchUpInside];
//    bookBottomView.hidden = YES;
//    [self.contentView addSubview:bookBottomView];
//    
//    [bookBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(kMargin);
//        make.right.mas_equalTo(self.contentView.mas_right).with.offset(- kMargin);
//        make.top.mas_equalTo(commentLabel.mas_bottom).with.offset(kHalfMargin);
//        make.height.mas_equalTo(kGeometricHeight(SCREEN_WIDTH, 3, 1) - kMargin);
//        make.bottom.mas_equalTo(self.contentView.mas_bottom).with.offset(- kHalfMargin).priorityLow();
//    }];
//    
//    bookImageView = [[TL_ProductionCoverView alloc] initWithProductionType:TL_ProductionTypeBook productionCoverDirection:TL_ProductionCoverDirectionVertical];
//    bookImageView.userInteractionEnabled = NO;
//    [bookBottomView addSubview:bookImageView];
//    
//    [bookImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(bookBottomView.mas_left).with.offset(kHalfMargin);
//        make.centerY.mas_equalTo(bookBottomView.mas_centerY);
//        make.height.mas_equalTo(bookBottomView.mas_height).with.offset(- kMargin);
//        make.width.mas_equalTo(kGeometricWidth(kGeometricHeight(SCREEN_WIDTH, 3, 1) - 2 * kMargin, 3, 4));
//    }];
//    
//    bookTitleLabel = [[UILabel alloc] init];
//    bookTitleLabel.textColor = kBlackColor;
//    bookTitleLabel.textAlignment = NSTextAlignmentLeft;
//    bookTitleLabel.numberOfLines = 1;
//    bookTitleLabel.font = kMainFont;
//    [bookBottomView addSubview:bookTitleLabel];
//    
//    [bookTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(bookImageView.mas_right).with.offset(kHalfMargin);
//        make.bottom.mas_equalTo(bookBottomView.mas_centerY);
//        make.right.mas_equalTo(bookBottomView.mas_right).with.offset(- kHalfMargin);
//        make.height.mas_equalTo(40);
//    }];
//    
//    authorLabel = [[UILabel alloc] init];
//    authorLabel.textColor = kGrayTextColor;
//    authorLabel.textAlignment = NSTextAlignmentLeft;
//    authorLabel.font = kFont10;
//    [bookBottomView addSubview:authorLabel];
//    
//    [authorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(bookTitleLabel.mas_left);
//        make.right.mas_equalTo(bookTitleLabel.mas_right);
//        make.height.mas_equalTo(30);
//        make.top.mas_equalTo(bookBottomView.mas_centerY);
//    }];
}
- (void)dzAction{
    
    [APIManager sendPostRequest:[APIURLManager likeGoods] parameters:@{@"commentId":@(self.commentsDetailModel.comment_id)} model:nil success:^(BOOL success, id  _Nullable responseData, APIResponse * _Nonnull requestModel) {
        
        if (success) {
            
            self.commentsDetailModel.likeStatus = !self.commentsDetailModel.likeStatus;
            
            if (self.commentsDetailModel.likeStatus) {
                [dzBtn setImage:[UIImage imageNamed:@"like-1-selected"] forState:UIControlStateNormal];
                self.commentsDetailModel.like_num = self.commentsDetailModel.like_num+1;
                commentNumLabel.text = [TL_UtilsHelper formatStringWithInteger:self.commentsDetailModel.like_num];
                commentNumLabel.textColor = [UIColor colorWithHexString:@"#FF9966"];
            }else{
                [dzBtn setImage:[UIImage imageNamed:@"like-1-unSelected"] forState:UIControlStateNormal];
                self.commentsDetailModel.like_num = self.commentsDetailModel.like_num-1;
                commentNumLabel.text = [TL_UtilsHelper formatStringWithInteger:self.commentsDetailModel.like_num];
                commentNumLabel.textColor = kGrayTextColor;
            }
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        
    }];
    
    
    
}

- (void)setAppraiseDetailModel:(TL_AppraiseDetailModel *)appraiseDetailModel
{
    _appraiseDetailModel = appraiseDetailModel;
    return;
    timeLabel.text = [NSString stringWithFormat:@"%@",appraiseDetailModel.time?:@""];
    
    [timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo([TL_ViewHelper getDynamicWidthWithLabel:timeLabel]);
    }];
    
    commentLabel.text = [NSString stringWithFormat:@"%@",appraiseDetailModel.content?:@""];
    
    [commentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo([TL_ViewHelper getDynamicHeightWithLabelFont:commentLabel.font labelWidth:(SCREEN_WIDTH - 35.0f - kMargin) labelText:commentLabel.text] - kHalfMargin);
    }];
    
    MK_WEAK_SELF
    [avatarImage setImageWithURL:[NSURL URLWithString:appraiseDetailModel.avatar?:@""] placeholder:avatarHoldImage options:YYWebImageOptionAvoidSetImage | YYWebImageOptionSetImageWithFadeAnimation completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        if (image) {
            UIImage *cornerImage = [image imageByResizeToSize:CGSizeMake(35.0, 35.0) contentMode:UIViewContentModeScaleAspectFill];
            cornerImage = [cornerImage imageByRoundCornerRadius:35.0 / 2.0];
            if (weakSelf) {
                SS(strongSelf)
                dispatch_async(dispatch_get_main_queue(), ^{
                    strongSelf->avatarImage.image = cornerImage;
                });
            }
        }
    }];
    
    nickNameLabel.text = [NSString stringWithFormat:@"%@",appraiseDetailModel.nickname?:@""];
    
    if (appraiseDetailModel.is_vip == 1) {
        [vipImageView setImage:[UIImage imageNamed:@"public_vip_select"]];
    } else {
        [vipImageView setImage:[UIImage imageNamed:@"public_vip_normal"]];
    }
    
    bookImageView.coverImageURL = appraiseDetailModel.cover;
    
    bookTitleLabel.text = appraiseDetailModel.name?:@"";
    
    authorLabel.text = appraiseDetailModel.author?:@"";
    
    if (appraiseDetailModel.reply_info.length > 0) {
        CGFloat replyHeight = [TL_ViewHelper getDynamicHeightWithLabelFont:SYSTEM_FONT(11) labelWidth:(SCREEN_WIDTH - 2 * kMargin - 35.0f - kHalfMargin) labelText:appraiseDetailModel.reply_info];
        NSString *replyInfo = [NSString stringWithFormat:@"%@",appraiseDetailModel.reply_info];
        
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.lineSpacing = 3;
        
        NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:replyInfo];
        [attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, replyInfo.length)];
        [attrString addAttribute:NSFontAttributeName value:SYSTEM_FONT(11) range:NSMakeRange(0, replyInfo.length)];
        
        replyCommentLabel.attributedText = attrString;
        [replyCommentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(avatarImage.mas_bottom).with.offset(kHalfMargin);
            make.height.mas_equalTo(replyHeight);
        }];
    } else {
        replyCommentLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:@""];
        [replyCommentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(avatarImage.mas_bottom);
            make.height.mas_equalTo(CGFLOAT_MIN);
        }];
    }
}

- (void)setProductionType:(TL_ProductionType)productionType
{
    [super setProductionType:productionType];
    
    bookImageView.productionType = productionType;
}

- (void)commentProductionClick
{
    if (self.commentProductionClickBlock) {
        self.commentProductionClickBlock();
    }
}

- (void)setCommentsDetailModel:(TL_CommentsDetailModel *)commentsDetailModel {
    _commentsDetailModel = commentsDetailModel;
    
    timeLabel.text = [NSDate convert2LocalTimeWithString:[NSString stringWithFormat:@"%@",_commentsDetailModel.time?:@""] andWithFormatter:@"MM-dd HH:mm"];
    
    commentLabel.text = [NSString stringWithFormat:@"%@",_commentsDetailModel.content?:@""];
    
    [commentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo([TL_ViewHelper getDynamicHeightWithLabelFont:commentLabel.font labelWidth:(SCREEN_WIDTH - 35.0f - kMargin) labelText:commentLabel.text] - kHalfMargin);
    }];
    
    commentNumLabel.text = [NSString stringWithFormat:@"%ld",_commentsDetailModel.like_num];
    
    if (_commentsDetailModel.likeStatus) {
        [dzBtn setImage:[UIImage imageNamed:@"like-1-selected"] forState:UIControlStateNormal];
        commentNumLabel.textColor = [UIColor colorWithHexString:@"#FF9966"];
    }else{
        [dzBtn setImage:[UIImage imageNamed:@"like-1-unSelected"] forState:UIControlStateNormal];
        commentNumLabel.textColor = kGrayTextColor;
    }
    
    
    MK_WEAK_SELF
    [avatarImage setImageWithURL:[NSURL URLWithString:_commentsDetailModel.avatar?:@""] placeholder:avatarHoldImage options:YYWebImageOptionAvoidSetImage | YYWebImageOptionSetImageWithFadeAnimation completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        if (image) {
            UIImage *cornerImage = [image imageByResizeToSize:CGSizeMake(35.0, 35.0) contentMode:UIViewContentModeScaleAspectFill];
            cornerImage = [cornerImage imageByRoundCornerRadius:35.0 / 2.0];
            if (weakSelf) {
                SS(strongSelf)
                dispatch_async(dispatch_get_main_queue(), ^{
                    strongSelf->avatarImage.image = cornerImage;
                });
            }
        }
    }];
    
    
    nickNameLabel.text = [NSString stringWithFormat:@"%@",_commentsDetailModel.nickname?:@""];
    
    if (_commentsDetailModel.is_vip == 1) {
        [vipImageView setImage:[UIImage imageNamed:@"public_vip_normal"]];
    } else {
        [vipImageView setImage:[UIImage imageNamed:@"public_vip_select"]];
    }
    
    bookImageView.coverImageURL = _commentsDetailModel.avatar;
    
    bookTitleLabel.text = _commentsDetailModel.nickname?:@"";
    
    authorLabel.text = _commentsDetailModel.author?:@"";
    
    if (_commentsDetailModel.reply_info.length > 0) {
        CGFloat replyHeight = [TL_ViewHelper getDynamicHeightWithLabelFont:SYSTEM_FONT(11) labelWidth:(SCREEN_WIDTH - 2 * kMargin - 35.0f - kHalfMargin) labelText:_commentsDetailModel.reply_info];
        NSString *replyInfo = [NSString stringWithFormat:@"%@",_commentsDetailModel.reply_info];
        
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.lineSpacing = 3;
        
        NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:replyInfo];
        [attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, replyInfo.length)];
        [attrString addAttribute:NSFontAttributeName value:SYSTEM_FONT(11) range:NSMakeRange(0, replyInfo.length)];
        
        replyCommentLabel.attributedText = attrString;
        [replyCommentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(avatarImage.mas_bottom).with.offset(kHalfMargin);
            make.height.mas_equalTo(replyHeight);
        }];
    } else {
        replyCommentLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:@""];
        [replyCommentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(avatarImage.mas_bottom);
            make.height.mas_equalTo(CGFLOAT_MIN);
        }];
    }
}

@end
